package com.dwd.hrm.service.impl;

import com.alibaba.fastjson.JSON;
import com.dwd.hrm.domain.Employee;
import com.dwd.hrm.domain.Tenant;
import com.dwd.hrm.mapper.EmployeeMapper;
import com.dwd.hrm.mapper.TenantMapper;
import com.dwd.hrm.service.IEmployeeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dwd.hrm.util.AjaxResult;
import com.dwd.hrm.vo.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wendi
 * @since 2021-03-02
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private TenantMapper tenantMapper;

    /**
     * 查询机构id和机构名称
     *
     * @param loginId
     * @return
     */
    @Override
    public AjaxResult loginInfo(Long loginId) {
        Employee employee = baseMapper.selectByLoginId(loginId);
        Tenant tenant = tenantMapper.selectById(employee.getTenantId());
        UserContext userContext = new UserContext();
        userContext.setLoginId(employee.getLoginId())
                .setTenantId(tenant.getId())
                .setTenantName(tenant.getCompanyName())
                .setUserId(employee.getId())
                .setUserName(employee.getUsername());
        String userContex = JSON.toJSONString(userContext);
        return AjaxResult.me().setResultObj(userContex);
    }
}
